﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GetFilePath
{
	public partial class ucGetFilePath : UserControl
	{
		private string strRootPath;

		public ucGetFilePath()
		{
			InitializeComponent();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (!CheckValid())
			{
				return;
			}
			txtDisplay.Text = GetFilePath(txtInputPath.Text);
		}

		private string GetFilePath(string strPath)
		{
			StringBuilder sb = new StringBuilder();
			DirectoryInfo dir = new DirectoryInfo(strPath);
			strRootPath = strPath;
			foreach (FileInfo file in dir.GetFiles())
			{
				sb.Append(file.FullName.Replace(strRootPath, "") + Environment.NewLine);
			}
			foreach (DirectoryInfo subdir in dir.GetDirectories())
			{
				GetFilePathInSubDirectory(sb, subdir.FullName);
			}
			return sb.ToString() ;
		}

		private void GetFilePathInSubDirectory(StringBuilder sb, string strPath)
		{
			DirectoryInfo dir = new DirectoryInfo(strPath);
			foreach (FileInfo file in dir.GetFiles())
			{
				sb.Append(file.FullName.Replace(strRootPath, "") + Environment.NewLine);
			}
			foreach (DirectoryInfo subdir in dir.GetDirectories())
			{
				GetFilePathInSubDirectory(sb, subdir.FullName);
			}
		}

		private bool CheckValid()
		{
			if (string.IsNullOrEmpty(txtInputPath.Text))
			{
				MessageBox.Show("请输入路径！");
				return false;
			}
			if (!Directory.Exists(txtInputPath.Text))
			{
				MessageBox.Show("输入的路径不存在！");
				return false;
			}
			return true;
		}

		private void btnClear_Click(object sender, EventArgs e)
		{
			Clear();
		}

		private void Clear()
		{
			txtInputPath.Text = txtDisplay.Text = String.Empty;
		}
	}
}
